cc.Class({
    extends: cc.Component,

    properties: {
      // wallNode:cc.Node,
      enemyPrefab:{
        default:null,
        type: cc.Prefab
      },
      showNumNode:{
        default:null,
        type:cc.Node,
      }
    },

  
    onLoad () {
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getCollisionManager().enabled = true;
        //cc.director.getCollisionManager().enabledDebugDraw = true;
        this.bgNode = this.node.getChildByName('bg');
        this.nowNumNode = this.showNumNode.getChildByName('item01')
        this.accountNode  = this.showNumNode.getChildByName('item02');
        this.countNum = this.bgNode.childrenCount;
       // this.showNumNode.active = true;
        
    }, 

    start () {
      this.amount = 0;
      this.killNum = 0;
      this.times =0;
      this.num = 1;
      this.getNewEnemy();
      this.showKillEnemyNum();
    },

    showKillEnemyNum:function(){
      this.showEnemyAcc(this.nowNumNode, this.killNum);
      if (this.killNum == this.amount) {
        cc.director.preloadScene('over',function(){
        setTimeout(() => {
          this.getLoadSence('over');
        }, 1500);
        }.bind(this));
      }
    },

    showEnemyAcc(node,string){
      this.accLabel = node.getComponent(cc.Label);
      this.accLabel.string = string
    },


    //生成怪物
    getNewEnemy:function () {
      var newNode = cc.instantiate(this.enemyPrefab);
      //var newNode = this.enemyPool.createEnemyPool(); //获取对象池中的对象
      this.bgNode.addChild(newNode);
      newNode.x = (Math.random()-0.5) * 1100;
      newNode.y = -240;
      this.amount ++ ;
      //newNode.getComponent('enemy').game = this;
    },

    getLoadSence(sence){
      cc.director.loadScene(sence);
    },

    // initMapNode(wallNode){
    //     let tiledMap = wallNode.getComponent(cc.TiledMap);
    //     let tiledSize = tiledMap.getTileSize();
    //     let layer = tiledMap.getLayer('wall');
    //     let layerSize = layer.getLayerSize();
    //     for (let i = 0; i < layerSize.width; i++) {
    //         for (let j = 0; j < layerSize.height; j++) {
    //             let tiled = layer.getTiledTileAt(i, j, true);
    //             if (tiled.gid != 0) {
    //                 tiled.node.group = 'map';
    //                 let body = tiled.node.addComponent(cc.RigidBody);
    //                 body.type = cc.RigidBodyType.Static;
    //                 let collider = tiled.node.addComponent(cc.PhysicsBoxCollider);
    //                 collider.offset = cc.v2(tiledSize.width / 2, tiledSize.height / 2);
    //                 collider.size =tiledSize;
    //                 collider.apply();
    //             }
             
    //         }
         
    //     }
    // },

    update (dt) {
        if (this.killNum == this.amount) {
           return;
        }
        this.times += dt;
        var newNum = 0;
        if (this.times > 0.3) {
          //this.node.removeFromParent();
          // console.log(enCou);
          if (this.num <=2) {
            this.getNewEnemy();
            this.num ++ ;
            this.countNum = this.bgNode.childrenCount;
          }
          this.showEnemyAcc(this.accountNode, "/ "+this.amount);
          //console.log(this.countNum);
          newNum = this.bgNode.childrenCount;
         // console.log(newNum);
          if (newNum <= this.countNum) {
              this.killNum = (this.countNum-newNum) % this.countNum;
              setTimeout(() => {
                this.showKillEnemyNum();
              }, 1500);
              
          }
          this.times = 0;
        }
       // console.log(132);
   }  
});
